package cn.hongxin.sap.conn.utils;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Properties;

/**
 * Created by gang.xu01@hand-china.com on 2018/12/4
 */
public class SAPConnUtils {

    private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";

    /**
     * 创建SAP接口属性文件。
     * @param name  ABAP管道名称
     * @param suffix    属性文件后缀
     * @param properties    属性文件内容
     */
    private static void createDataFile(String name, String suffix, Properties properties){
        File cfg = new File(name+"."+suffix);
        if(cfg.exists()){
            cfg.deleteOnExit();
        }
        try{
            FileOutputStream fos = new FileOutputStream(cfg, false);
            properties.store(fos, "for tests only !");
            fos.close();
        }catch (Exception e){
            System.out.println("Create Data file fault, error msg: " + e.toString());
            throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
        }
    }

    /**
     * 初始化SAP连接
     */
    private static void initProperties(String propertiesName,Map<String, Object> connectMap) {
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, connectMap.get("hostaddr")+"");//主机 例如：192.168.1.123
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  connectMap.get("sysnumber")+"");//系统号例如： "01"
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, connectMap.get("clientnumber")+"");//客户端例如： "300"
        connectProperties.setProperty(DestinationDataProvider.JCO_USER, connectMap.get("username")+""); //用户名 userName
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, connectMap.get("password")+"");//密码 password
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, connectMap.get("language")+"");//语种例如："EN"
        connectProperties.setProperty(DestinationDataProvider.JCO_MAX_GET_TIME, "180000");//超时
        String minActive = connectMap.get("minactive")+"";
        if(StringUtils.isNotBlank(minActive)){
            connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, minActive);
        }
        String maxActive = connectMap.get("maxactive")+"";
        if(StringUtils.isNotBlank(maxActive)){
            connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, maxActive);
        }

        createDataFile(propertiesName, "jcoDestination", connectProperties);
    }

    /**
     * 获取SAP连接
     * @return  SAP连接对象
     */
    public static JCoDestination connect(String propertiesName,Map<String, Object> connectMap){
        System.out.println("正在连接至SAP...");
        JCoDestination destination = null;
        initProperties(propertiesName,connectMap);
        try {
            destination = JCoDestinationManager.getDestination(propertiesName);
            destination.ping();
            System.out.println("已成功建立sap的连接");
        } catch (JCoException e) {
            System.out.println("Connect SAP fault, error msg: " + e.toString());
            throw new RuntimeException("Connect SAP fault, error msg: " + e.toString());
        }
        return destination;
    }


    /**
     * 测试SAP连接
     * @return  测试SAP连接
     */
    public static String connecting(String propertiesName,Map<String, Object> connectMap){
        String result = "";
        System.out.println("正在连接至SAP...");
        JCoDestination destination = null;
        initProperties(propertiesName,connectMap);
        try {
            destination = JCoDestinationManager.getDestination(propertiesName);
            destination.ping();
            result = "success";
        } catch (JCoException e) {
            System.out.println("Connect SAP fault, error msg: " + e.toString());
            result = e.getMessage();
            throw new RuntimeException("Connect SAP fault, error msg: " + e.toString());
        }
        return result;
    }

}
